package com.samoyed.credit.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.samoyed.credit.Modle.ListDataModle;
import com.samoyed.credit.R;

import java.util.ArrayList;

/**
 * @author dsh
 * @date : 2021/10/30 9:58
 * @description:
 */
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public void setList(ArrayList<ListDataModle> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private ArrayList<ListDataModle> list;
    private Context context;

    public MyAdapter(ArrayList<ListDataModle> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == 1) {
            //展示时间的item
            View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
            viewHolder = new ViewHolder1(view);
        }
        if (viewType == 2) {
            //展示内容的item
            View view = LayoutInflater.from(context).inflate(R.layout.item_layout2, parent, false);
            viewHolder = new ViewHolder2(view);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = list.get(position).getType();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });

        if (type == 1) {
            ((ViewHolder1) holder).tv_time.setText(list.get(position).getTime());
            ((ViewHolder1) holder).tv_amount.setText("¥"+list.get(position).getAmount());
        }
        if (type == 2) {
            //本笔账单的记账类型 1:代表手动编写 2:代表语音输入 3:：代表AI识别
            int orderType = list.get(position).getAccountingtype();
            switch (orderType) {

                case 2:
                    Glide.with(context).load(R.mipmap.yuyinshuruzhong).into(((ViewHolder2) holder).img_type);
                    break;

                case 3:
                    Glide.with(context).load(R.mipmap.paizhao).into(((ViewHolder2) holder).img_type);
                    break;

                default:
                    Glide.with(context).load(R.mipmap.bianji).into(((ViewHolder2) holder).img_type);
                    break;

            }

            ((ViewHolder2) holder).tv_content.setText(list.get(position).getOrdercontent());

            //本笔订单类型 1代表收入 2代表支出
            int ordertype = list.get(position).getOrdertype();

            switch (ordertype) {
                case 1:
                    ((ViewHolder2) holder).tv_amount.setText("+ " + list.get(position).getAmount());
                    break;

                case 2:
                    ((ViewHolder2) holder).tv_amount.setText("- " + list.get(position).getAmount());
                    break;
            }

        }
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {


        private TextView tv_time;
        private TextView tv_amount;


        public ViewHolder1(View itemView) {
            super(itemView);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_amount = (TextView) itemView.findViewById(R.id.tv_amount);
        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {

        private ImageView img_type;
        private TextView tv_content;
        private TextView tv_amount;

        public ViewHolder2(View itemView) {
            super(itemView);
            img_type = (ImageView) itemView.findViewById(R.id.img_type);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_amount = (TextView) itemView.findViewById(R.id.tv_amount);

        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener listener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
